package com.llollox.openharmonyprojects.compoundbuttongroup;

import ohos.agp.components.*;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;

import static com.llollox.openharmonyprojects.compoundbuttongroup.CompoundButtonGroup.CompoundType.CHECK_BOX;
import static com.llollox.openharmonyprojects.compoundbuttongroup.CompoundButtonGroup.CompoundType.RADIO;

/**
 * Created by rigatol.
 */
public class FullWidthCompoundButton extends DirectionalLayout {
    private static final String TAG = "FullWidthCompoundButton";
    private static final int TEXT_SIZE = 50;
    private String value;
    private Text textView;
    private CheckRadioView mCheckRadioView;
    private CustomCheckBox mCustomCheckBox;
    private CompoundButtonGroup.LabelOrder labelOrder = CompoundButtonGroup.LabelOrder.BEFORE;
    private CompoundButtonGroup.CompoundType viewType = CHECK_BOX;
    private Context context;
    private Listener listener;

    public FullWidthCompoundButton(Context context) {
        super(context);
        init(context, null);
    }

    public FullWidthCompoundButton(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttrSet attrs) {
        this.context = context;
        setClickable(true);
        setOrientation(HORIZONTAL);
        setPadding(15, 15, 15, 15);

        textView = new Text(context);

        prepareButton();
        addOrderedViews(labelOrder);
    }

    String getValue() {
        return value;
    }

    void setValue(String value) {
        this.value = value;
    }

    boolean isChecked() {
        if (viewType == RADIO) {
            return this.mCheckRadioView.isChecked();
        } else {
            return this.mCustomCheckBox.isChecked();

        }
    }

    void setChecked(boolean isChecked) {
        if (viewType == RADIO) {
            this.mCheckRadioView.setChecked(isChecked);
        } else if (viewType == CHECK_BOX) {
            this.mCustomCheckBox.setChecked(isChecked);
        }
    }

    void setLabelOrder(CompoundButtonGroup.LabelOrder labelOrder) {
        this.labelOrder = labelOrder;
        refresh();
    }

    void setCompoundType(CompoundButtonGroup.CompoundType viewType) {
        this.viewType = viewType;
        refresh();
    }

    void setText(String text) {
        textView.setText(text);
    }

    void setListener(Listener listener) {
        this.listener = listener;
    }

    private void addOrderedViews(CompoundButtonGroup.LabelOrder labelOrder) {
        switch (labelOrder) {
            case BEFORE:
                DirectionalLayout.LayoutConfig params = new LayoutConfig(
                        LayoutConfig.MATCH_PARENT,
                        LayoutConfig.MATCH_PARENT);
                params.weight = 1.0f;
                textView.setLayoutConfig(params);
                textView.setTextSize(TEXT_SIZE);
                textView.setTextAlignment(TextAlignment.VERTICAL_CENTER);
                addComponent(textView);
                if (viewType == RADIO) {
                    addComponent(mCheckRadioView);
                } else if (viewType == CHECK_BOX) {
                    addComponent(mCustomCheckBox);
                }
                break;

            case AFTER:
                if (viewType == RADIO) {
                    addComponent(mCheckRadioView);
                } else if (viewType == CHECK_BOX) {
                    addComponent(mCustomCheckBox);
                }
                DirectionalLayout.LayoutConfig paramss = new LayoutConfig(
                        LayoutConfig.MATCH_CONTENT,
                        LayoutConfig.MATCH_CONTENT);
                textView.setLayoutConfig(paramss);
                textView.setTextSize(TEXT_SIZE);
                addComponent(textView);
                break;
        }
    }

    private void prepareButton() {
        switch (viewType) {
            case CHECK_BOX:
                this.mCustomCheckBox = new CustomCheckBox(context);
                mCustomCheckBox.setLayoutConfig(new ComponentContainer.LayoutConfig(
                        100,
                        100));
                mCustomCheckBox.setClickable(false);
                break;
            case RADIO:
                this.mCheckRadioView = new CheckRadioView(context);
                mCheckRadioView.setLayoutConfig(new ComponentContainer.LayoutConfig(
                        100,
                        100));
                mCheckRadioView.setClickable(false);
                break;
            default:
                LogUtil.error(TAG, "Unknown Component Type");
        }
        setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (listener != null) {
                    listener.onButtonClicked(component);
                }
                if (mCustomCheckBox != null) {
                    mCustomCheckBox.setChecked(!mCustomCheckBox.isChecked());
                }
                if (mCheckRadioView != null) {
                    mCheckRadioView.setChecked(!mCheckRadioView.isChecked());
                }
            }
        });
    }


    private void refresh() {
        removeAllComponents();
        prepareButton();
        addOrderedViews(labelOrder);
    }

    interface Listener {
        void onButtonClicked(Component component);
    }
}
